package homework7.soso.untils;

import homework7.soso.entity.MobileCard;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

/**
 * @author fengq
 * 数字操作的工具类
 */
public class NumberUtil {

    /**
     * 生成随机卡号
     * @return
     */
    public static String createNumber(Map<String, MobileCard> cards, Scanner input) {
        System.out.println("*******可选择的卡号***********");
        Random random = new Random();
        String[] number = new String[9];
        //标志位
        boolean isExist = true;
        int i = 1;
        do {
            do {
                //产生一个随机数
                int b = (random.nextInt(90000000) + 10000000);
                //利用字符串拼接产生一个号码
                number[i - 1] = "139" + b;
                if (!cards.containsKey(number[i - 1])) {
                    System.out.print(i + "." + number[i - 1] + "\t");
                    isExist = false;
                    if (i % 3 == 0) {
                        System.out.println();
                    }
                    i++;
                } else {
                    isExist = true;
                }
            } while (isExist);
        } while (i != 10);
        System.out.println("请选择卡号(1-9)");
        int is;
        do {
            if (input.hasNextInt()) {
                is = input.nextInt();
                break;
            } else {
                System.out.println("输入有误，请重新输入");
                input.next();
            }
        } while (true);
        return number[is - 1];
    }

    /**
     * double类型格式装换
     * @param data
     * @return
     */
    public static String dataFormat(double data) {
        DecimalFormat format = new DecimalFormat("#.0");
        return format.format(data);
    }
}
